﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Models
{
    public class OrganizationModel
    {
        private ISession _session;

        public IList<Organization> GetOrganizations()
        {
            IList<Organization> organizations;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from Organization where UsersId='" + Client.Instance.ClientId + "';");
                organizations = query.List<Organization>();
                foreach (Organization organization in organizations)
                {
                    IQuery query1 =
                        _session.CreateQuery("from Project where OrganizationID='" + organization.Id + "';");
                    List<Project> projects = query1.List<Project>().ToList();
                    if (projects.Any())
                    {
                        organization.ProjectCollection = projects;
                    }
                    else
                    {
                        organization.ProjectCollection = new List<Project>();
                    }
                }
            }
            return organizations;
        }

        public Organization GetOrganizationById(Guid id)
        {
            var organization = new Organization();
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    organization = _session.Get<Organization>(id);
                    tran.Commit();
                }
            }
            return organization;
        }

        public bool NewOrganization(Organization organization)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Save(organization);
                    tran.Commit();
                }
            }
            return true;
        }

        public void UpdateOrganization(Organization organization)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Update(organization);
                    tran.Commit();
                }
            }
        }

        public void DeleteOrganization(Organization organization)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    try
                    {
                        _session.Delete(organization);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                }
            }
        }
    }
}